#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void Print(int* p, int n)
{
	while (n--)
	{
		printf("%d ", *(p++));
	}
	printf("\n");
}

void Swap(int* x, int* y)
{
	int z = *x;
	*x = *y;
	*y = z;
}

void InsertSort(int* p, int n)
{
	int i = 0;
	while (i < n - 1)
	{
		int end = i;
		int tmp = p[end + 1];
		while (end >= 0)
		{
			if (p[end] > tmp)
			{
				p[end + 1] = p[end];
				end--;
			}
			else
			{
				break;
			}
			p[end + 1] = tmp;
		}
		i++;
	}
}

void ShellSort(int* p, int n)
{
	int gap = n;
	while (gap > 1)
	{
		gap /= 2;
		for (int i = 0; i < n - gap; i++)
		{
			int end = i;
			int tmp = p[gap + end];
			while (end >= 0)
			{
				if (p[end] < tmp)
				{
					p[end + gap] = p[end];
					end -= gap;
				}
				else
				{
					break;
				}
			}
			p[end + gap] = tmp;
		}
	}
}

void AdjustDoen(int* p, int n, int root)
{
	int parent = root;
	int child = parent * 2 + 1;
	while (child < n)
	{
		if (child + 1 < n && p[child + 1] > p[child])
		{
			child += 1;
		}
		if (p[child] > p[parent])
		{
			Swap(&p[child], &p[parent]);
			parent = child;
			child = parent * 2 + 1;
		}
		else
		{
			break;
		}
	}
}

void HeapSort(int* p, int n)
{
	for (int i = (n - 1 - 1) / 2; i >= 0; i--)
	{
		AdjustDoen(p, n, i);
	}
	int end = n - 1;
	while (end > 0)
	{
		Swap(&p[0], &p[end]);
		AdjustDoen(p, end, 0);
		--end;
	}
}

void SelectSort(int* p, int n)
{
	int begin = 0;
	int end = n - 1;
	while (begin < end)
	{
		int min = begin;
		int max = end;
		for (int i = begin; i <= end; i++)
		{
			if (p[i] < p[min])
			{
				min = i;
			}
			if (p[i] > p[max])
			{
				max = i;
			}
			Swap(&p[min], &p[end]);
			if (max == end)
			{
				max = min;
			}
			Swap(&p[max], &p[begin]);
		}
		begin++;
		end--;
	}
}

void BubbleSort(int* p, int n)
{
	for (int x = 0; x < n - 1; x++)
	{
		int exchange = 0;
		for (int y = 0; y < n - x - 1; y++)
		{
			if (p[y] > p[y + 1])
			{
				Swap(&p[y], &p[y + 1]);
				exchange = 1;
			}
		}
		if (exchange == 0)
		{
			break;
		}
	}
}

int GetMidIndex(int* p, int left, int right)
{
	int mid = (left + right) >> 1;
	if (p[left] < p[right])
	{
		if (p[left] > p[mid])
		{
			return left;
		}
		else if (p[mid] > p[right])
		{
			return right;
		}
		else
		{
			return mid;
		}
	}
	else
	{
		if (p[left] < p[mid])
		{
			return left;
		}
		else if (p[mid] < p[right])
		{
			return right;
		}
		else
		{
			return mid;
		}
	}
}

void QuickSort(int* p, int left,int right)
{
	if (left >= right)
	{
		return;
	}
	int index = GetMidIndex(p, left, right);
	Swap(&p[left], &p[index]);
	int begin = left;
	int end = right;
	int pivot = begin;
	int key = p[begin];
	while (begin < end)
	{
		while (end > begin && p[end] <= key)
		{
			end--;
		}
		p[pivot] = p[end];
		pivot = end;
		while (begin < end && p[begin] >= key)
		{
			begin++;
		}
		p[pivot] = p[begin];
		pivot = begin;
	}
	p[begin] = key;
	QuickSort(p, 0, begin - 1);
	QuickSort(p, begin + 1, right);
}

void _MergeSort(int* p, int left, int right, int* tmp)
{
	if (left >= right)
	{
		return;
	}
	int mid = (left + right) >> 1;
	_MergeSort(p, left, mid, tmp);
	_MergeSort(p, mid + 1, right, tmp);
	int begin1 = left;
	int begin2 = mid + 1;
	int index = left;
	while (begin1 <= mid && begin2 <= right)
	{
		if (p[begin1] < p[begin2])
		{
			tmp[index++] = p[begin1++];
		}
		else
		{
			tmp[index++] = p[begin2++];
		}
	}
	while (begin1 <= mid)
	{
		tmp[index++] = p[begin1++];
	}
	while (begin2 <= right)
	{
		tmp[index++] = p[begin2++];
	}
	for (int i = left; i <= right; i++)
	{
		p[i] = tmp[i];
	}
}

void MergeSort(int* p, int n)
{
	int* tmp = (int*)malloc(sizeof(int) * n);
	_MergeSort(p, 0, n - 1, tmp);
	free(tmp);
}

int main()
{
	int p[] = { 9,8,7,6,5,4,3,2,1 };
	InsertSort(p, 9);
	Print(p, 9);
	ShellSort(p, 9);
	Print(p, 9);
	HeapSort(p, 9);
	Print(p, 9);
	SelectSort(p, 9);
	Print(p, 9);
	BubbleSort(p, 9);
	Print(p, 9);
	QuickSort(p, 0, 8);
	Print(p, 9);
	MergeSort(p, 9);
	Print(p, 9);
	return 0;
}